package com.example.demo.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Orderlist;
import com.example.demo.entity.Product;
import com.example.demo.entity.User;
import com.example.demo.mapper.OrderlistMapper;
import com.example.demo.mapper.ProductMapper;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;

@RestController
@RequestMapping("/orderlist")
public class OrderlistController extends BaseController {

    @Resource
    OrderlistMapper OrderlistMapper;

    @Resource
    ProductMapper productMapper;

    @PostMapping
    public Result<?> save(@RequestBody Orderlist Orderlist) {
        OrderlistMapper.insert(Orderlist);
        return Result.success();
    }

    @PutMapping
    public Result<?> update(@RequestBody Orderlist Orderlist) {
        Date date=new Date();
        Orderlist.setPayTime(date);
        OrderlistMapper.updateById(Orderlist);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Integer id) {
        OrderlistMapper.deleteById(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<?> getById(@PathVariable Integer id) {
        return Result.success(OrderlistMapper.selectById(id));
    }

    @GetMapping("/buy/{productid}")
    public Result<?> buy(@PathVariable Integer productid,
                         @RequestParam(defaultValue = "1") Integer amount,
                         @RequestParam(defaultValue = "1") Integer userId,
                         @RequestParam(defaultValue = "1") String username) {
        Product product = productMapper.selectById(productid);
        String orderNo = IdUtil.getSnowflake().nextIdStr();
        Orderlist orderlist = new Orderlist();
        orderlist.setOrderNo(orderNo);
        BigDecimal bigAmount = new BigDecimal(amount.toString());
        orderlist.setTotalPrice(product.getPrice().multiply(bigAmount));
        orderlist.setProductid(productid);
        orderlist.setAmount(amount);
        orderlist.setUserId(userId);
        orderlist.setUsername(username);
        orderlist.setName(product.getName());
        orderlist.setState(0);
        save(orderlist);
        // 新建订单，扣减库存
        return Result.success();
    }

    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {
        LambdaQueryWrapper<Orderlist> wrapper = Wrappers.lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(Orderlist::getUsername, search);
        }
        Page<Orderlist> OrderPage = OrderlistMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(OrderPage);
    }
}
